<?php

class tournament
{
	public $TABLE_NAME = 'Tournaments';
	public $TABLE_COLUMNS = array( 
			'id'					=> 'Tournament_id',
			'name'					=> 'Name',
			'admin_id'				=> 'Admin_id',
			'registration_fee'		=> 'Registration_fee',
			'bet'					=> 'Bet',
			'start_time'			=> 'Start_time',
			'registration_time'		=> 'Registration_time',
			'checkin'				=> 'Checkin_counter_time',
			'game'					=> 'Id_game',
			'matchup'				=> 'Matchup_format',
			'players_max'			=> 'Players_max',
			'players_registered'	=> 'Players_registered',
			'points_min'			=> 'Points_min',
			'points_max'			=> 'Points_max',
			'details'				=> 'Details',
			'chat_log'				=> 'Chat_log',
			'status'				=> 'Status'
		);
		
	private $DB = null;
		
	function __construct( $Dblink )
	{
		if ( $Dblink ) 	
		{
			$this->DB = $Dblink;
		}
		else
		{
			return false;
		}		
	}	
	
	function add( $Data )
	{
		$tournament_add_sql = "
			INSERT INTO
				`%s`
			(
				%s	
			)
			VALUES
			(
				%s	
			)";
		
		$columns = '';
		$values = '';
			
		foreach( $Data as $k=>$d )	
		{
			$columns .= sprintf( "`%s`, ", $this->TABLE_COLUMNS[$k] );
			$values .= sprintf( "'%s', ", $this->DB->db_real_escape_string( $d ) );
		}
		
		$columns = substr( $columns, 0, -2 );
		$values = substr( $values, 0, -2 );
		
		$tournament_add_sql = sprintf( 
				$tournament_add_sql,  
				$this->TABLE_NAME,
				$columns,
				$values
			);
		$tournament_add_result = $this->DB->db_query( $tournament_add_sql );
		if ( $tournament_add_result['insert_id'] )
		{
			return $tournament_add_result['insert_id'];
		}
		else
		{
			return false;
		}	
	}
	
	function get_list( $Status = 0 )
	{
		$tournament_list_sql = sprintf( "
				SELECT
					*
				FROM
					`%s`
				WHERE
					`%s`='%d'",
				$this->TABLE_NAME,
				$this->TABLE_COLUMNS['status'],
				$Status
			);
		$tournament_list_result = $this->DB->db_query( $tournament_list_sql );
		if ( $tournament_list_result['num_rows'] )
		{
			return $tournament_list_result['data'];
		}
		else
		{
			return false;
		}
	}
	
	function delete( $Tournament_id )
	{
		$Tournament_id = (int)$Tournament_id;
		if ( $Tournament_id )
		{
			$tournament_delete_sql = sprintf( "
				DELETE FROM
					`%s`
				WHERE
					`%s`='%d'",
				$this->TABLE_NAME,
				$this->TABLE_COLUMNS['id'],
				$Tournament_id
				 );
			$tournament_delete_result = $this->DB->db_query( $tournament_delete_sql );
			if ( $tournament_delete_result['affected_rows'] )
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	function modifiy( $Tournament_id, $Data )
	{
		$Tournament_id = (int)$Tournament_id;
		if ( $Tournament_id )
		{
			$tournament_modify_sql = sprintf( "
				UPDATE 
					`%s` 
				SET ",
				$this->TABLE_NAME );
			
			$sets = '';
			foreach( $Data as $k=>$v )
			{
				$sets .=  sprintf( "`%s`='%s', ", $this->TABLE_COLUMNS[$k], $v );
			}
			$sets = substr( $sets, 0, -2 );
			
			$tournament_modify_sql .= sprintf( "
						%s 
						WHERE 
							`%s`='%d'", 
						$sets,
						$this->TABLE_COLUMNS['id'],
						$Tournament_id
					);
			$tournament_modify_result = $this->DB->db_query( $tournament_modify_sql );					if ( $tournament_modify_result['affected_rows'] )
			{
				return true;
			}
			else
			{
				return false;
			}		
		}
		else
		{
			return false;
		}
	}
	
	function get_id( $Tournament_id )
	{
		if ( $Tournament_id )
		{	
			$tournament_sql = sprintf( "
				SELECT
					*
				FROM
					`%s`
				WHERE
					`%s`='%d'
				",
				$this->TABLE_NAME,
				$this->TABLE_COLUMNS['id'],
				$Tournament_id
				 );
			$tournament_result = $this->DB->db_query( $tournament_sql );
			if ( $tournament_result['num_rows'] )
			{
				return $tournament_result['data'][0];
			}
			else
			{
				return array();
			}
		 }
		 else
		 {
		 	return false;
		 }
	}	
	
}

?>